<?php
class Thesismodel extends CI_Model{
	function __construct(){
		parent::__construct();
	}
	
	//return all thesis
	function getAll(){
		$this->db->select('*');
		$this->db->from('thesis');
		$this->db->join('user', 'teacher_id=user_id');
		
		$query = $this->db->get();
		if ($query->num_rows() > 0){
			foreach ($query->result() as $row){
				$data[] = $row;
			}
			return $data;
		}
		else 
			$data[] = false;
		return $data;
	}
	
	//search thesis by attribute: name
	function searchByName($attribute){
		$this->db->select('*');
		$this->db->from('thesis');
		$this->db->join('user', 'user_id=teacher_id');
		$this->db->like('name',$attribute);
		$query = $this->db->get();
		if ($query->num_rows() > 0){
			foreach ($query->result() as $row){
				$data[] = $row;
			}
			return $data;
		}
		else 
			$data[] = false;
		return $data;
	}
	
	//search thesis by attribute: id
	function searchById($attribute){
		$this->db->select('*');
		$this->db->from('thesis');
		$this->db->join('user', 'user_id = teacher_id');
		$this->db->where('thesis_id',$attribute);
		$query = $this->db->get();
		if ($query->num_rows() > 0){
			foreach ($query->result() as $row){
				$data[] = $row;
			}
			return $data;
		}
		else 
			$data[] = false;
		return $data;
	}
	
	//search thesis by attribute: type
	function searchByType($attribute){
		$this->db->select('*');
		$this->db->from('thesis');
		$this->db->join('user', 'user_id = teacher_id');
		$this->db->like('thesisType',$attribute);
		$query = $this->db->get();
		if ($query->num_rows() > 0){
			foreach ($query->result() as $row){
				$data[] = $row;
			}
			return $data;
		}
		else 
			$data[] = false;
		return $data;
	}
	
	//search thesis by attribute: teacher name
	function searchByTeachername($attribute){
		$this->db->select('*');
		$this->db->from('thesis');
		$this->db->join('user', 'user_id = teacher_id');
		$this->db->like('firstname',$attribute);
		$this->db->or_like('lastname',$attribute);
		$token = strtok($attribute, " ");
		while ($token != false){
			$this->db->like('firstname',$token);
			$this->db->or_like('lastname',$token);
			$token = strtok(" ");
		}
		$query = $this->db->get();
		if ($query->num_rows() > 0){
			foreach ($query->result() as $row){
				$data[] = $row;
			}
			return $data;
		}
		else 
			$data[] = false;
		return $data;
	}	
	
	//return topic by teacher id
	function getTopic($attribute){
		$sql = "SELECT * FROM thesis WHERE teacher_id = $attribute";
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0){
			foreach ($query->result() as $row){
				$data[] = $row;
			}
			return $data;
		}
		else 
			$data[] = false;
		return $data;
	}

	//update modified information to database
	function update($id,$data){
		$this->db->where('thesis_id', $id);
		$this->db->update('thesis', $data); 
	}
	
	//delete thesis
	function delete($topicId){
	//	$topicId = $this->session->userdata('topicId');
		$sql = "DELETE FROM thesis WHERE thesis_id = $topicId";
		$this->db->query($sql);
	}
	
	//delete thesises of a teacher
	function deleteByIdTeacher($id){
		$sql = "DELETE FROM thesis WHERE teacher_id = $id";
		$this->db->query($sql);
	}
	
	//add thesis
	function add(){
		$name= $this->input->post('name');
		$content= $this->input->post('description');
		$type= $this->input->post('type');
		$quantity= $this->input->post('quantity');
		$teacherId = $this->session->userdata('user_id');
		$area = $this->session->userdata('area');
		$sql = "INSERT INTO thesis (name,description,thesisType, quantity_students,teacher_id,researchArea) 
		VALUES('$name','$content','$type','$quantity','$teacherId','$area')";
		$this->db->query($sql);	
	}
	
	//check owner
	function checkOwner($topicId){
		$teacherId = $this->session->userdata('user_id');
		$sql = "SELECT * FROM thesis WHERE teacher_id = '$teacherId' AND thesis_id = '$topicId'";
		$query = $this->db->query($sql);
		
		if ($query->num_rows() > 0)
			return true;
		return false;
	}
	
	//update quantity of students after teacher decide
	// -1 - enroll, 0 - quit, 1 - accept, 2 - reject
	function updateQuantity($deci,$sta){
		$topicId = $this->session->userdata('topicId');
		$sql = "NULL";	
		//student enroll
		if ($deci == -1 && $sta == 1){
			$sql = "UPDATE thesis SET waiting = waiting + 1 WHERE thesis_id = $topicId";
		//student quit before teacher decide, after enroll
		} else if ($deci == 0 && $sta == 1){
			$sql = "UPDATE thesis SET waiting = waiting - 1 WHERE thesis_id = $topicId";
		//student quit after teacher accept
		}else if ($deci == 0 && $sta == 2){
			$sql = "UPDATE thesis SET accepted = accepted - 1  WHERE thesis_id = $topicId";
		//student quit after teacher reject
		}else if ($deci == 0 && $sta == 3){
		//	$sql = "UPDATE thesis SET waiting = waiting - 1 WHERE thesis_id = $topicId";
		//	$sql = "NULL";
		//teacher accept when student waiting
			return;
		}else if ($deci == 1 && $sta == 1){
			$sql = "UPDATE thesis SET waiting = waiting - 1,accepted = accepted + 1 WHERE thesis_id = $topicId";
		//teacher accept after reject
		}else if ($deci == 1 && $sta == 3){
			$sql = "UPDATE thesis SET accepted = accepted + 1 WHERE thesis_id = $topicId";
		//teacher reject when student waiting
		}else if ($deci == 2 && $sta == 1){
			$sql = "UPDATE thesis SET waiting = waiting - 1 WHERE thesis_id = $topicId";
		//teacher reject after accept
		}else if ($deci == 2 && $sta == 2){
			$sql = "UPDATE thesis SET accepted = accepted - 1 WHERE thesis_id = $topicId";
		}
		
		if ($sql == "NULL")
			return;
		else
			$this->db->query($sql);
	}
	
	//return registered topic of a student
	function registeredTopic($studentId){
		$this->db->select('*');
		$this->db->from('thesis');
		$this->db->join('list_students', 'list_students.thesis_id=thesis.thesis_id');
		$this->db->join('user', 'teacher_id=user_id');
		$this->db->where('student_id',$studentId);
		
		$query = $this->db->get();
		if ($query->num_rows() > 0){
			foreach ($query->result() as $row){
				$data[] = $row;
			}
			return $data;
		}
		else 
			$data[] = false;
		return $data;
	}
}
?>